import _ from 'lodash';

/**
 * 浏览器
 * @Author: wenjm 
 * @Date: 2018-03-13 23:51:34 
 * @Last Modified by: wenjm
 * @Last Modified time: 2018-06-08 14:51:12
 */
class Viewer {

    constructor(fw) {
        this.$framework = fw;
        this.timer = undefined;
    }

    getWebview() {
        return window.plus.webview.getWebviewById("webviewer");
    }

    openURL(url, styles, extras, listeners) {
        var current = this.getWebview();
        if (current) {
            current.close();
        }
        this.$framework.$f7.preloader.show();
        var topoffset = this.$framework.$f7.device.ios ? "45px" : "135px";
        if (window.plus.navigator.isImmersedStatusbar()) {
            topoffset = (Math.round(window.plus.navigator.getStatusbarHeight()) + 45) + 'px';
        }
        styles = _.extend({}, {
            top: topoffset,
            bottom: '0px',
            position: 'dock',
            dock: 'bottom',
            bounce: 'vertical',
            scalable: true
        }, styles);
        current = window.plus.webview.create(url, "webviewer", styles, extras);
        current.appendJsFile('_www/libs/plugins/ajaxhook.js');
        current.addEventListener('loaded', (e) => {
            if (this.timer) {
                window.clearTimeout(this.timer);
            }
            if (_.isString(e) && e.toString().indexOf("dcloud_error.html") > -1) {
                if (this.$framework.$f7.views.current.router.nextPath == "/viewer/web/") {
                    this.$framework.$f7.router.back();
                }
                this.$framework.$f7.dialog.alert("网页打开失败，请稍后重试");
                this.close();
            } else {
                if (this.$framework.$f7.views.current.router.nextPath != "/viewer/web/") {
                    this.$framework.$f7.router.navigate("/viewer/web/");
                } else {
                    this.$framework.getController("/viewer/web/").query('form');
                }
                current.show();
            }
            this.$framework.$f7.preloader.hide();
        }, false);

        if (listeners) {
            _.each(listeners, function (f, k) {
                current.addEventListener(k, f, false);
            });
        }

        this.timer = window.setTimeout(() => {
            current.close();
            this.$framework.$f7.preloader.hide();
            this.$framework.$f7.dialog.alert("网页打开失败，请稍后重试");
        }, 10000);
        return current;
    }


    openApp(url, styles, extras, listeners) {
        var current = this.getWebview();
        if (current) {
            current.close();
        }
        this.$framework.$f7.dialog.progress('正在启动应用中...');
        var topoffset = "0px";
        if (window.plus.navigator.isImmersedStatusbar()) {
            topoffset = (Math.round(window.plus.navigator.getStatusbarHeight())) + 'px';
        }
        styles = _.extend({}, {
            top: topoffset,
            bottom: '0px',
            position: 'dock',
            dock: 'bottom',
            bounce: 'vertical',
            scalable: true
        }, styles);
        url += (url.indexOf("?") > -1 ? "&" : "?") + "type=inner";
        current = window.plus.webview.create(url, "webviewer", styles, extras);
        current.appendJsFile('_www/libs/plugins/ajaxhook.js');
        current.addEventListener('loaded', (e) => {
            if (this.timer) {
                window.clearTimeout(this.timer);
            }
            if (_.isString(e) && e.toString().indexOf("dcloud_error.html") > -1) {
                this.$framework.$f7.dialog.alert("网页打开失败，请稍后重试");
                this.$framework.viewer.close();
            } else {
                current.show();
            }
            this.$framework.$f7.dialog.close();
            this.$framework.$f7.preloader.hide();
        }, false);

        if (listeners) {
            _.each(listeners, (f, k) => {
                current.addEventListener(k, f, false);
            });
        }

        this.timer = window.setTimeout(() => {
            current.close();
            this.$framework.$f7.preloader.hide();
            this.$framework.$f7.dialog.alert("网页打开失败，请稍后重试");
        }, 10000);
        return current;
    }

    openPDF() {

    }

    openFile() {

    }

    openBrowser(url) {
        if (url) {
            if (window.plus) {
                window.plus.runtime.openURL(url);
            } else {
                window.open(url);
            }
        }
    }

    close() {
        var current = this.getWebview();
        if (current) {
            current.close();
        }
    }

}

export default Viewer;